<?php

namespace App\Http\Controllers\Admin\Order;

use App\Exceptions\OrderException;
use App\Services\Admin\Order\SponsorOrderService;
use Exception;
use Illuminate\Http\JsonResponse;

class SponsorOrderController extends BaseOrderController
{

    protected SponsorOrderService $sponsorOrderService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->sponsorOrderService = $this->loadService('Admin\Order\SponsorOrderService');
    }

    /**
     * 认养订单列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->sponsorOrderService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 认养订单详情
     * @return JsonResponse
     * @throws OrderException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->sponsorOrderService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 更新订单状态
     * @return JsonResponse
     * @throws OrderException
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'orderStatus' => 'required|integer|in:30',
            'remark' => 'required|string'
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorOrderService->update($this->params);
        return outputJsonResult();
    }

}
